package com.hdoit.apollo.web.shop.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Cargo;
import com.hdoit.apollo.logical.cargo.model.Collocation;
import com.hdoit.apollo.logical.cargo.service.ICargoService;
import com.hdoit.apollo.logical.cargo.service.ICollocationService;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.model.SupplyCargo;
import com.hdoit.apollo.logical.shop.service.ISupplyCargoService;
import com.hdoit.apollo.logical.viewmodel.cargo.CollocationSearch;
import com.hdoit.apollo.logical.viewmodel.shop.SupplyCargoSearch;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年05月22日 11:59:36
 * @comment
 */
@Controller
@RequestMapping(value = "/shop/supplyCargo/")
public class SupplyCargoController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/shop/supplyCargo/";
    }

    @Autowired
    private ISupplyCargoService supplyCargoService;
    
    @Autowired
    private ICargoService cargoService;
    
    @Autowired
    private ICollocationService collocationService;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="supplyCargoDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String changePriceViewName="change";
    
    private final String collocationViewName="collocation/index";
    
    
    @RequestMapping(value = changePriceViewName, method = { RequestMethod.POST},produces="text/plain;charset=utf-8")
    public @ResponseBody String change(String id,String supplyPrice ){
    	
    	if(!StringUtils.isEmpty(id)){
    		SupplyCargo supplyCargo=new SupplyCargo();
    		supplyCargo=supplyCargoService.queryModel(id);
    		
        	supplyCargo.setSupplyPrice(Double.parseDouble(supplyPrice));
        	supplyCargo.setModifyDate(DateUtils.getCurrent());
        	supplyCargo.setModifyUserId(getLoginModel().getLoginId());
        	supplyCargo.setModifyUserName(getLoginModel().getLoginName());
        	supplyCargo.setModelStatus(ModelStatus.update);
    		if(supplyCargoService.applyChange(supplyCargo)){
        		return new JsonActionResult(ManageContainer.SUCCESS).toJson();
    		}else{
    			return new JsonActionResult(ManageContainer.ERROR).toJson();
    		}
    	}
    	
    	return new JsonActionResult(ManageContainer.WARNING).toJson();
    }
    
    @RequestMapping(value = collocationViewName, method = { RequestMethod.GET,RequestMethod.POST})
    public String collocationIndex(Model model,String supplierId,CollocationSearch search,PagerModel pagerModel,boolean ajaxPost){
    	pagerModel.setPageSize(10);
    	List<Collocation> collocations = collocationService.queryListMP(search,pagerModel);
    	System.err.println(search.getCargoName()+"胡茂树");
    	model.addAttribute("collocations", collocations);
        pagerModel.setTotalAmount(collocationService.queryCountMP(search));
        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        
        
        model.addAttribute("supplierId", supplierId);
        model.addAttribute("collocationSearch", search);
        
        
        if(ajaxPost){
            return view("collocation/output");
        }
        return view(collocationViewName);
    }

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,String supplierId, PagerModel pagerModel,SupplyCargoSearch search,boolean ajaxPost){

    	System.err.println(supplierId+"咩咩咩咩咩咩");
    	search.setSupplierId(supplierId);
    	
        pagerModel.setTotalAmount(supplyCargoService.queryCountMP(search));
        List<SupplyCargo> supplyCargos= supplyCargoService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("supplyCargos", supplyCargos);
        model.addAttribute("supplyCargoSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String supplyCargoDetailed(Model model,String key){
        SupplyCargo supplyCargo=new SupplyCargo();
        if(!StringUtils.isEmpty(key)){
            supplyCargo=supplyCargoService.queryModel(key);
        }
        model.addAttribute("supplyCargo", supplyCargo);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST},produces="text/plain;charset=utf-8")
    public @ResponseBody String save(Model model,String supplierId,String[] collocationIds){
    	
    	Shop shop =(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
    	
    	if(collocationIds.length>0){
    		List<SupplyCargo> supplyCargos =new ArrayList<SupplyCargo>();
    		SupplyCargo supplyCargo=null;
    		
    		for (int i = 0; i < collocationIds.length; i++) {
    			supplyCargo=new SupplyCargo();
    			supplyCargo.setSupplierId(supplierId);
    			supplyCargo.setCollocationId(collocationIds[i]);
    			
    			Cargo cargo=new Cargo();
    			Collocation collocation=new Collocation();
    			cargo=cargoService.queryModelByCollocationId(collocationIds[i]);
    			collocation=collocationService.queryModel(collocationIds[i]);
    			supplyCargo.setCargoId(cargo.getCargoId());
    			supplyCargo.setId(CommonUtils.getKeyId());
    			if (StringUtils.isEmpty(collocation.getPurchasePrice())) {
    				supplyCargo.setSupplyPrice(collocation.getCostPrice());
				}else {
					supplyCargo.setSupplyPrice(collocation.getPurchasePrice());
				}
    			
    			
    			supplyCargo.setCreateDate(DateUtils.getCurrent());
    			supplyCargo.setDeleteMark(YesOrNoInteger.no);
    			supplyCargo.setModelStatus(ModelStatus.add);
    			supplyCargo.setCreateUserId(getLoginModel().getLoginId());
    			supplyCargo.setCreateUserName(getLoginModel().getLoginName());
    			supplyCargo.setShopId(shop.getShopId());
    			supplyCargos.add(supplyCargo);
			}
    		
    		if(supplyCargoService.applyChanges(supplyCargos)){
        		return new JsonActionResult(ManageContainer.SUCCESS).toJson();
    		}else{
    			return new JsonActionResult(ManageContainer.ERROR).toJson();
    		}
    		
    	}
    	
    	return new JsonActionResult(ManageContainer.WARNING).toJson();
    	


    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(supplyCargoService.deleteSupplyCargoByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}